<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${sqlmap.entityTypeSimpleName?uncap_first}">

	<!-- auto generate default resultMap -->
	<resultMap id="${sqlmap.entityTypeSimpleName?uncap_first}Map"
		type="${sqlmap.entityTypeName}">
<#list sqlmap.propertyList as property>
	<#if !property.isSimpleValueType()>
		<result column="${property.columnName}" property="${property.columnPropertyName}"/>
	<#elseif property.columnName != property.columnPropertyName>
		<result column="${property.columnName}" property="${property.columnPropertyName}"/>
	<#else>
	</#if>
</#list>
	</resultMap>

	<!-- auto generate default find -->
	<select id="find"
		parameterType="${sqlmap.entityTypeName}"
		resultMap="${sqlmap.entityTypeSimpleName?uncap_first}Map">
		SELECT
<#list sqlmap.propertyList as property>
				${sqlmap.simpleTableName}.${property.columnName}<#if property_has_next>,</#if>
</#list>
		  FROM ${sqlmap.tableName} ${sqlmap.simpleTableName}
		 WHERE
		<trim prefixOverrides="AND | OR">
<#if sqlmap.pkProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.pkProperty.propertyName})">
	            AND ${sqlmap.simpleTableName}.${sqlmap.pkProperty.columnName} = ${r"#{"}${sqlmap.pkProperty.columnPropertyName}${r"}"}
	        </if>
</#if>
<#if sqlmap.codeProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.codeProperty.propertyName})">
	            AND ${sqlmap.simpleTableName}.${sqlmap.codeProperty.columnName} = ${r"#{"}${sqlmap.codeProperty.columnPropertyName}${r"}"}
	        </if>
</#if>
		</trim>
	</select>

	<!-- auto generate default query -->
	<select id="query"
		parameterType="java.util.Map"
		resultMap="${sqlmap.entityTypeSimpleName?uncap_first}Map">
		SELECT
<#list sqlmap.propertyList as property>
				${sqlmap.simpleTableName}.${property.columnName}<#if property_has_next>,</#if>
</#list>
		  FROM ${sqlmap.tableName} ${sqlmap.simpleTableName}
		<trim prefix="WHERE" prefixOverrides="AND | OR">
			<if test="ew.expression.normal != null and !ew.expression.normal.isEmpty()">
				<![CDATA[ AND ${r"${ew.expression.normal.sqlSegment}"} ]]>
			</if>
<#if sqlmap.parentIdProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentIdProperty.propertyName}s)">
	            AND ${sqlmap.simpleTableName}.${sqlmap.parentIdProperty.columnName} IN <foreach collection="${sqlmap.parentIdProperty.propertyName}s" open="(" close=")" separator="," item="parentIdTemp">${r"#{parentIdTemp}"}</foreach>
	        </if>
</#if>
<#if (sqlmap.parentProperty?? && !(sqlmap.parentIdProperty??))>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentProperty.propertyName}Ids)">
	            AND ${sqlmap.simpleTableName}.${sqlmap.parentProperty.columnName} IN <foreach collection="parentIds" open="(" close=")" separator="," item="parentIdTemp">${r"#{parentIdTemp}"}</foreach>
	        </if>
	        <if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentProperty.propertyName}Id)">
	            AND ${sqlmap.simpleTableName}.${sqlmap.parentProperty.columnName} = ${r"#{"}${sqlmap.parentProperty.propertyName}Id${r"}"}
	        </if>
	        <if test="${sqlmap.parentProperty.propertyName} != null">
	        	<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentProperty.propertyName}.id)">
		            AND ${sqlmap.simpleTableName}.${sqlmap.parentProperty.columnName} = ${r"#{"}${sqlmap.parentProperty.propertyName}.id${r"}"}
		        </if>
	        </if>
</#if>
<#list sqlmap.propertyList as property>
	<#if property.propertyName == "createDate">
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(minCreateDate)">
	            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} >= ${r"#{"}minCreateDate${r"} ]]>"}
	        </if>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(maxCreateDate)">
	            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} < ${r"#{"}maxCreateDate${r"} ]]>"}
	        </if>
	<#elseif property.propertyTypeName == "java.util.Date" || property.propertyTypeName == "java.sql.Date">
	<#elseif property.propertyTypeName == "float" || property.propertyTypeName == "java.lang.Float">
	<#elseif property.propertyTypeName == "double" || property.propertyTypeName == "java.lang.Double">
	<#elseif !property.isSimpleValueType()>
			<if test="${property.propertyName} != null">
				<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${property.columnPropertyName})">
		            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} = ${r"#{"}${property.columnPropertyName}${r"} ]]>"}
		        </if>
	        </if>
	<#else>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${property.propertyName})">
	            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} = ${r"#{"}${property.propertyName}${r"} ]]>"}
	        </if>
	</#if>
</#list>
<#if sqlmap.pkProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(exclude${sqlmap.pkProperty.propertyName?cap_first})">
				${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${sqlmap.pkProperty.columnName} ${r"<>"} ${r"#{exclude"}${sqlmap.pkProperty.propertyName?cap_first}${r"} ]]>"}
			</if>
</#if>
		</trim>
		<choose>
			<when test="ew.expression.orderBy != null and !ew.expression.orderBy.isEmpty()">
				${r"${"}ew.expression.orderBy.sqlSegment${r"}"}
	        </when>
	        <otherwise>
	            ORDER BY ${sqlmap.defaultOrderBy}
	        </otherwise>
	    </choose>
	</select>

	<!-- auto generate default count -->
	<select id="queryCount"
		parameterType="java.util.Map"
		resultType="java.lang.Integer">
		SELECT COUNT(1)
		  FROM ${sqlmap.tableName} ${sqlmap.simpleTableName}
		<trim prefix="WHERE" prefixOverrides="AND | OR">
			<if test="ew.expression.normal != null and !ew.expression.normal.isEmpty()">
				<![CDATA[ AND ${r"${ew.expression.normal.sqlSegment}"} ]]>
			</if>
<#if sqlmap.parentIdProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentIdProperty.propertyName}s)">
	            AND ${sqlmap.simpleTableName}.${sqlmap.parentIdProperty.columnName} IN <foreach collection="${sqlmap.parentIdProperty.propertyName}s" open="(" close=")" separator="," item="parentIdTemp">${r"#{parentIdTemp}"}</foreach>
	        </if>
</#if>
<#if (sqlmap.parentProperty?? && !(sqlmap.parentIdProperty??))>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentProperty.propertyName}Ids)">
	            AND ${sqlmap.simpleTableName}.${sqlmap.parentProperty.columnName} IN <foreach collection="parentIds" open="(" close=")" separator="," item="parentIdTemp">${r"#{parentIdTemp}"}</foreach>
	        </if>
	        <if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentProperty.propertyName}Id)">
	            AND ${sqlmap.simpleTableName}.${sqlmap.parentProperty.columnName} = ${r"#{"}${sqlmap.parentProperty.propertyName}Id${r"}"}
	        </if>
	        <if test="${sqlmap.parentProperty.propertyName} != null">
	        	<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.parentProperty.propertyName}.id)">
		            AND ${sqlmap.simpleTableName}.${sqlmap.parentProperty.columnName} = ${r"#{"}${sqlmap.parentProperty.propertyName}.id${r"}"}
		        </if>
	        </if>
</#if>
<#list sqlmap.propertyList as property>
	<#if property.propertyName == "createDate">
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(minCreateDate)">
	            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} >= ${r"#{"}minCreateDate${r"} ]]>"}
	        </if>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(maxCreateDate)">
	            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} < ${r"#{"}maxCreateDate${r"} ]]>"}
	        </if>
	<#elseif property.propertyTypeName == "java.util.Date" || property.propertyTypeName == "java.sql.Date">
	<#elseif property.propertyTypeName == "float" || property.propertyTypeName == "java.lang.Float">
	<#elseif property.propertyTypeName == "double" || property.propertyTypeName == "java.lang.Double">
	<#elseif !property.isSimpleValueType()>
			<if test="${property.propertyName} != null">
				<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${property.columnPropertyName})">
		            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} = ${r"#{"}${property.columnPropertyName}${r"} ]]>"}
		        </if>
	        </if>
	<#else>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${property.propertyName})">
	            ${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${property.columnName} = ${r"#{"}${property.propertyName}${r"} ]]>"}
	        </if>
	</#if>
</#list>
<#if sqlmap.pkProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(exclude${sqlmap.pkProperty.propertyName?cap_first})">
				${r"<![CDATA[ "}AND ${sqlmap.simpleTableName}.${sqlmap.pkProperty.columnName} ${r"<>"} ${r"#{exclude"}${sqlmap.pkProperty.propertyName?cap_first}${r"} ]]>"}
			</if>
</#if>
		</trim>
	</select>

	<!-- auto generate default insert -->
    <insert id="insert"
		parameterType="${sqlmap.entityTypeName}">
		INSERT INTO ${sqlmap.tableName}
		(
<#list sqlmap.propertyList as property>
	<#if property.isInsertable()>
			${property.columnName}<#if property_has_next>,</#if>
	</#if>
</#list>
		)
		VALUES
		(
<#list sqlmap.propertyList as property>
	<#if property.isInsertable()>
			${r"#{"}${property.columnPropertyName}${r"}"}<#if property_has_next>,</#if>
	</#if>
</#list>
		)
	</insert>

	<!-- auto generate default delete -->
	<delete id="delete"
		parameterType="${sqlmap.entityTypeName}">
		DELETE FROM ${sqlmap.tableName}  WHERE
		<trim prefixOverrides="AND | OR">
<#if sqlmap.pkProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.pkProperty.propertyName})">
	            AND ${sqlmap.pkProperty.columnName} = ${r"#{"}${sqlmap.pkProperty.columnPropertyName}${r"}"}
	        </if>
</#if>
<#if sqlmap.codeProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.codeProperty.propertyName})">
	            AND ${sqlmap.codeProperty.columnName} = ${r"#{"}${sqlmap.codeProperty.columnPropertyName}${r"}"}
	        </if>
</#if>
		</trim>
	</delete>

	<!-- auto generate default update -->
	<update id="update"
	    parameterType="java.util.Map">
	    UPDATE ${sqlmap.tableName}
	    <trim prefix="SET" suffixOverrides=",">
			<if test="et != null and ew == null">
<#list sqlmap.propertyList as property>
	<#if (!property.isPrimaryKey() && property.isUpdatable() && property.isSimpleValueType() && !property.isPrimitiveType()
	&& property.propertyName != "createTime" && property.propertyName != "createDate"
	&& property.propertyName != "createOperatorId" && property.propertyName != "createOperator"
	&& property.propertyName != "createUserId" && property.propertyName != "createUser"
	&& property.propertyName != "code")>
				<if test="!_parameter.containsKey('${property.propertyName}') and et.${property.columnPropertyName} != null">
					${property.columnName} = ${r"#{"}et.${property.columnPropertyName}${r"}"},
				</if>
	</#if>
</#list>
			</if>
<#list sqlmap.propertyList as property>
	<#if (!property.isPrimaryKey() && property.isUpdatable()
		&& property.propertyName != "createTime" && property.propertyName != "createDate"
		&& property.propertyName != "createOperatorId" && property.propertyName != "createOperator"
		&& property.propertyName != "createUserId" && property.propertyName != "createUser"
		&& property.propertyName != "code")>
			<if test="_parameter.containsKey('${property.propertyName}')">
	    		${property.columnName} = ${r"#{"}${property.columnPropertyName},javaType=${property.columnPropertyTypeName}${r"}"},
	    	</if>
	</#if>
</#list>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(ew.sqlSet)">
				${r"${ew.sqlSet}"},
			</if>
	    </trim>
	    WHERE
		<trim prefixOverrides="AND | OR">
			<if test="ew.expression.normal != null and !ew.expression.normal.isEmpty()">
				AND ${r"${ew.expression.normal.sqlSegment}"}
			</if>
<#if sqlmap.pkProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.pkProperty.propertyName})">
	            AND ${sqlmap.pkProperty.columnName} = ${r"#{"}${sqlmap.pkProperty.columnPropertyName}${r"}"}
	        </if>
</#if>
<#if sqlmap.codeProperty??>
			<if test="@com.tx.core.util.OgnlUtils@isNotEmpty(${sqlmap.codeProperty.propertyName})">
	            AND ${sqlmap.codeProperty.columnName} = ${r"#{"}${sqlmap.codeProperty.columnPropertyName}${r"}"}
	        </if>
</#if>
		</trim>
	</update>

</mapper>